﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.Reflection;
using System.IO;

namespace ModBot.ConfigBuilder
{
    

    public class SharedLib
    {
        public static string NewLine = "\r\n";
        public static string IniCommentDelimeter = ";";
        public static char CommentSeperator = '*';
        private const int subCommentLineWidth = 40;
        private const int commentLineWidth = 80;

        public enum ClassName
        {
            Enchanter,
            Wizard,
            Magician,
            Necromancer,
            Cleric,
            Druid,
            Shaman,
            Paladin,
            Shadowknight,
            Warrior,
            Beastlord,
            Ranger,
            Berserker,
            Rogue,
            Bard,
            Monk
        }

        public enum ClassShortName
        {
            ENC,
            WIZ,
            MAG,
            NEC,
            CLR,
            DRU,
            SHM,
            PAL,
            SHD,
            WAR,
            BST,
            RNG,
            BER,
            ROG,
            BRD,
            MNK
        }

        public static ClassShortName ConvertClassNameToShortName(ClassName c)
        {
            return ((ClassShortName)(int)c);
        }

        public static ClassName ConvertClassShortNameToName(ClassShortName c)
        {
            return ((ClassName)(int)c);
        }

        public static Boolean ConvertToBoolean(string value, Boolean defaultValue)
        {
            Boolean valueAsBoolean = defaultValue;
            if (!Boolean.TryParse(value, out valueAsBoolean))
            {
                return defaultValue;
            }

            return valueAsBoolean;
        }

        public static Int32 ConvertToInt32(string value, Int32 defaultValue)
        {
            Int32 valueAsInt32 = defaultValue;
            if (!Int32.TryParse(value, out valueAsInt32))
            {
                return defaultValue;
            }

            return valueAsInt32;
        }      

        public static void MoveSelectedListBoxItemUp(ListBox box)
        {
            
            int selectedIndex = box.SelectedIndex;
            if (selectedIndex > box.Items.Count) return;
            if (selectedIndex < 0) return;

            object swap = box.SelectedItem;      
            if (selectedIndex > 0)
            {
                box.Items.RemoveAt(selectedIndex);
                box.Items.Insert(selectedIndex - 1, swap);
                box.SelectedItem = swap;
            }
        }

        public static void MoveSelectedListBoxItemDown(ListBox box)
        {
            int selectedIndex = box.SelectedIndex;          //Selected Index
            if (selectedIndex < 0) return;

            object swap = box.SelectedItem;      //Selected Item
            if (selectedIndex < box.Items.Count-1)
            {
                box.Items.RemoveAt(selectedIndex);
                box.Items.Insert(selectedIndex + 1, swap);
                box.SelectedItem = swap;
            }
        }

        public static void UncheckListboxItems(CheckedListBox box)
        {
            for (int i = 0; i < box.Items.Count; i++)
            {
                box.SetItemChecked(i, false);
            }
        }

        public static void CheckListboxItems(CheckedListBox box)
        {
            for (int i = 0; i < box.Items.Count; i++)
            {
                box.SetItemChecked(i, true);
            }
        }


        public static string GetSummaryForProperty(Type type, string propertyName)
        {
            Assembly assembly = type.Assembly;
            PropertyInfo propertyInfo = type.GetProperty(propertyName);

            if(!File.Exists(assembly.GetName().Name + ".XML"))
            {
                return string.Empty;
            }


            XmlDocument xml = new XmlDocument();
            xml.Load(assembly.GetName().Name + ".XML");


            
            string fullName = "P:" + propertyInfo.DeclaringType.FullName + "." + propertyInfo.Name;

            XmlNodeList xnList = xml.SelectNodes("/doc/members/member");
            foreach (XmlNode node in xnList)
            {
                if (node.Attributes["name"].Value == fullName)
                {
                    if (node["summary"].InnerText != null)
                    {
                        string nodeSummary = node["summary"].InnerText;
                        nodeSummary = nodeSummary.Trim();
                        while (nodeSummary.Contains("  "))
                        {
                            nodeSummary = nodeSummary.Replace("  ", " ");
                        }

                        nodeSummary = nodeSummary.Replace("\r\n ", "\r\n");
                        return nodeSummary;
                    }
                    return string.Empty;
                }
            }           

            return string.Empty;
        }

        public static void NavigateUrl(string url)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.FileName = url;            
            proc.Start();
        }

        public static string AssemblyVersion
        {
            get
            {
                return Assembly.GetExecutingAssembly().GetName().Version.ToString();
            }
        }

       
        public static string CreateSubCommentBlock(string comment, string newLineConstant, Boolean alignRight)
        {
            return CreateCommentBlock(comment, NewLine, subCommentLineWidth, alignRight);
        }

        public static string CreateSubCommentBlock(string comment, string newLineConstant)
        {
            return CreateCommentBlock(comment, NewLine, subCommentLineWidth, false);
        }

        public static string CreateCommentBlock(string comment, string newLineConstant, Boolean alignRight)
        {
            return CreateCommentBlock(comment, NewLine, commentLineWidth, alignRight);
        }

        public static string CreateCommentBlock(string comment, string newLineConstant)
        {
            return CreateCommentBlock(comment, NewLine, commentLineWidth, false);
        }

        private static string CreateCommentBlock(string comment, string newLineConstant, int padWidth, Boolean alignRight)
        {
            string commentBlock =
            SharedLib.IniCommentDelimeter + "".PadRight(padWidth, SharedLib.CommentSeperator) + SharedLib.NewLine;

            string[] commentLines = comment.Split(newLineConstant.ToCharArray());

            for(int i = 0; i < commentLines.Length; i++)
            {
                string line = commentLines[i];
                if (alignRight)
                {
                    line = line.PadLeft(padWidth - 4, ' ');
                }
                else
                {
                    line = line.PadRight(padWidth - 4, ' ');
                }
                commentBlock += SharedLib.IniCommentDelimeter + "* " + line + " *" + SharedLib.NewLine;
            }

            commentBlock +=
                SharedLib.IniCommentDelimeter + "".PadRight(padWidth, SharedLib.CommentSeperator) + SharedLib.NewLine;

            return commentBlock;                
        }

        public static string SpellFilePath
        {
            get
            {
                return Properties.Settings.Default.SpellFilePath;
            }
            set
            {
                Properties.Settings.Default.SpellFilePath = value;
            }
        }

        public static string TextDriverFormatText
        {
            get
            {
                return Properties.Settings.Default.TextDriverFormatText;
            }
            set
            {
                Properties.Settings.Default.TextDriverFormatText = value;
            }
        }

        public static string MyClass
        {
            get
            {
                return Properties.Settings.Default.MyClass;
            }
            set
            {
                Properties.Settings.Default.MyClass = value;
            }

        }

        public static DialogResult GetDeleteConfirmation()
        {
            return MessageBox.Show("Are you sure you wish to delete this record?", "Delete Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
        }
    }
}
